from sklearn.preprocessing import MinMaxScaler
from sklearn.preprocessing import StandardScaler
from sklearn.preprocessing import LabelEncoder
from sklearn.preprocessing import OrdinalEncoder
from sklearn.preprocessing import OneHotEncoder
from sklearn.preprocessing import Binarizer
from sklearn.preprocessing import KBinsDiscretizer
import pandas as pd
import numpy as np

data=[[0.5,101],[0.3,86],[0.6,154],[0.2,53],[0.3,91]]
data=pd.DataFrame(data)
print(data)

scaler=MinMaxScaler()
scaler=scaler.fit(data)
result = scaler.transform(data)
print(result)
result_inverse=scaler.inverse_transform(result)
print(result_inverse)
result2=MinMaxScaler().fit_transform(data)
print(result2)
# feature_range可以限定归一化的范围
result3=MinMaxScaler(feature_range=[1,100]).fit_transform(data)
print(result3)

# scaler=StandardScaler()
# scaler=scaler.fit(data)
# result = scaler.transform(data)
# print(result)
# print('标准化后的均值：',round(result.mean(),3))
# print('标准化后的方差：',result.std())
# result_inverse=scaler.inverse_transform(result)
# print(result_inverse)
# result2=StandardScaler().fit_transform(data)
# print(result2)


y=[[1,'a'],[3,'b'],[3,'c'],[4,'a'],[5,'c']]
data2=pd.DataFrame(y)
print(data2)
lb=LabelEncoder()
lbf=lb.fit(data2[1])
print(lbf.classes_)
data2['transform']=lbf.transform(data2[1])
data2['inverse_transform']=lbf.inverse_transform(data2['transform'])
print(data2)
data2['fit_transform']=LabelEncoder().fit_transform(data2[1])
print(data2)

data3=pd.DataFrame(y)
oe=OrdinalEncoder()
oef=oe.fit(data3[1].values.reshape(-1, 1))
print(oef.categories_)
data3['transform']=oef.transform(data3[1].values.reshape(-1, 1))
data3['inverse_transform']=oef.inverse_transform(data3['transform'].values.reshape(-1, 1))
print(data3)
data3['fit_transform']=OrdinalEncoder().fit_transform(data3[1].values.reshape(-1, 1))
print(data3)

# LabelEncoder和OrdinalEncoder有什么区别？
# 1. 结果一样
# 2. 数值类型不一样，LabelEncoder是int64，OrdinalEncoder是float64
# 3. OrdinalEncoder可以一次处理多列，LabelEncoder只能处理单列
data=[[0.5,101],[0.3,86],[0.6,154],[0.2,53],[0.3,91]]
data=pd.DataFrame(data)
data['LabelEncoder']=LabelEncoder().fit_transform(data[1])
data['OrdinalEncoder']=OrdinalEncoder().fit_transform(data[1].values.reshape(-1,1))
data[['OrdinalEncoder1','OrdinalEncoder2']]=pd.DataFrame(OrdinalEncoder().fit_transform(data[[0,1]]))
print(data)






y2=[[1,'a','male'],[3,'b','female'],[3,'c','male'],[4,'a','male'],[5,'c','female']]
data4=pd.DataFrame(y2)
oh=OneHotEncoder()
ohf=oh.fit(data4[[1,2]])
data_oh=pd.DataFrame(ohf.transform(data4[[1,2]]).toarray(),columns=ohf.get_feature_names())
data4=pd.concat([data4,data_oh],axis=1)
print(data4)
print(OneHotEncoder().fit_transform(data4[[1,2]]).toarray())

# OneHotEncoder().fit_transform()是不是不能get_feature_names了？


data=[[0.5,101],[0.3,86],[0.6,154],[0.2,53],[0.3,91]]
data=pd.DataFrame(data)
bz=Binarizer(threshold=100)
bzf=bz.fit(data[1].values.reshape(-1, 1))
data['transform']=bzf.transform(data[1].values.reshape(-1, 1))

# Binarizer没有inverse_transform
# data['inverse_transform']=bzf.inverse_transeform(data['transform'].values.reshape(-1, 1))
data['fit_transform']=Binarizer(threshold=100).fit_transform(data[1].values.reshape(-1,1))
print(data)